﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Main_Inventory_PurchaseOrder_ShortClose : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            PoShortClose.Visible = true;
            //DivIssueItemShortClose.Visible = false;
            GetDatafromPo();
        }
    }
    protected void GetDatafromPo()
    {
        string Sql = "select ISNULL(Poprefix,'')+'/'+ISNULL(povoucherno,'')+'/'+ISNULL(posuffix,'') as PONO,ACC_POMAIN.POID as POID, ShortCloseDate, shortclose_remark, ISNULL(ACC_POMAIN.ShortClose,0) as ShortClose,PODate,TotalQty,DeliveryDate,GrandTotal,ACC_LEDMAST.LNAME as VenderName from IVR_TRAN_MAIN right outer join ACC_POMAIN on ACC_POMAIN.POID=IVR_TRAN_MAIN.POID inner join ACC_LEDMAST on ACC_POMAIN.LID=ACC_LEDMAST.LID where ACC_POMAIN.stages=4 and ACC_POMAIN.Shortclose=0 order by povoucherno";
        DataTable Dt = DU.getdata(Sql);
        if (Dt.Rows.Count > 0)
        {
            grdposhortclose.DataSource = Dt;
            grdposhortclose.DataBind();
        }
    }

    //protected void grdposhortclose_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    grdposhortclose.PageIndex = e.NewPageIndex;
    //    GetDatafromPo();
    //}
    protected void btnShortClose_Click(object sender, EventArgs e)
    {        
        foreach (GridViewRow row in grdposhortclose.Rows)
        {
            CheckBox ChkStatus = (CheckBox)row.FindControl("chkshortclose");
            HiddenField HidVal = (HiddenField)row.FindControl("hidPOID");
            if (ChkStatus.Checked == true && ChkStatus.Enabled == true)
            {
                string SqlShortClose = "Update Acc_Pomain set ShortClose=1,ShortCloseDate=GETDATE(), shortclose_Remark='" + txtremarks.Text + "' where PoID='" + HidVal.Value + "'";
                int Update = DU.executesql(SqlShortClose);
                if (Update > 0)
                {
                    lblmsg.ForeColor = System.Drawing.Color.Green;
                    lblmsg.Text = "Short Close of Purchase Order is Assigned.";
                    txtremarks.Text = "";
                }
            }
        }
        GetDatafromPo();
    }

    protected void grdposhortclose_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable DTChildPO = new DataTable();
            DTChildPO.Columns.Add("ItemCode", typeof(string));
            DTChildPO.Columns.Add("ItemName", typeof(string));
            DTChildPO.Columns.Add("ItemQty", typeof(string));
            DTChildPO.Columns.Add("ItemReceive", typeof(string));
            DTChildPO.Columns.Add("BalanceItem", typeof(string));

            GridView GrdPOChild = (GridView)e.Row.FindControl("gvPOShortChild");
            HiddenField LblPoid = (HiddenField)e.Row.FindControl("hidPOID");
            string PORecSql = "select IM.ITMCODE as ItemCode,IM.ITMDESC as ItemName,I.ITMQTY as ItemRec From IVR_TRAN_MAIN M inner join IVR_TRAN_ITEM I on M.TRNID=I.TRNID inner join IVR_ITEM IM on I.ITMID=IM.ITMID where M.POID='" + LblPoid.Value + "'";
            string POQty = "select ItemQty from ACC_PODETAILS where PoId='" + LblPoid.Value + "'";
            DataTable DtPOItemChild = DU.getdata(PORecSql);
            DataTable DTPoItemQtyPO = DU.getdata(POQty);
            if (DtPOItemChild.Rows.Count > 0)
            {
                for (int i = 0; i < DtPOItemChild.Rows.Count; i++)
                {
                    DataRow DR = DTChildPO.NewRow();
                    DR["ItemCode"] = DtPOItemChild.Rows[i]["ItemCode"].ToString();
                    DR["ItemName"] = DtPOItemChild.Rows[i]["ItemName"].ToString();
                    DR["ItemReceive"] = Convert.ToDouble(DtPOItemChild.Rows[i]["ItemRec"].ToString()).ToString("N0");//For Purchase Receive Qty
                    if (DTPoItemQtyPO.Rows.Count > 0)
                    {
                        for (int j = 0; j < DTPoItemQtyPO.Rows.Count; j++)
                        {
                            DR["ItemQty"] = Convert.ToDouble(DTPoItemQtyPO.Rows[j]["ItemQty"].ToString()).ToString("N0");//For Purchase Order Qty
                            DR["BalanceItem"] = Convert.ToDouble(Convert.ToDouble(DTPoItemQtyPO.Rows[j]["ItemQty"].ToString()) - Convert.ToDouble(DtPOItemChild.Rows[i]["ItemRec"].ToString())).ToString("N0");//For balance Qty
                        }
                    }
                    DTChildPO.Rows.Add(DR);
                }
            }
            GrdPOChild.DataSource = DTChildPO;
            GrdPOChild.DataBind();
            CheckBox chk = (CheckBox)e.Row.FindControl("chkshortclose");
            if (chk.Checked == true)
            {
                chk.Enabled = false;
            }
        }
    }
    protected void chkshortclose_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in grdposhortclose.Rows)
        {
            CheckBox ChkStatus = (CheckBox)row.FindControl("chkshortclose");
            //HiddenField RowId = (HiddenField)row.FindControl("hidrowid");
            AjaxControlToolkit.ModalPopupExtender AJAXMPE = (AjaxControlToolkit.ModalPopupExtender)row.FindControl("MPE");
            if (ChkStatus.Checked == true && ChkStatus.Enabled == true)
            {
                hidrowid.Value = (row.RowIndex + 1).ToString();
                AJAXMPE.Show();
            }
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        AjaxControlToolkit.ModalPopupExtender AJAXISSUE = (AjaxControlToolkit.ModalPopupExtender)grdposhortclose.Rows[0].FindControl("MPE");
        AJAXISSUE.Hide();
        txtremarks.Text = "";
        GetDatafromPo();
    }
}